package com.tianyu.jty.system.utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class uploadFile extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf-8");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String savePath = this.getServletConfig().getServletContext()
				.getRealPath("");
		savePath = savePath + "/uploadFile/";
		DiskFileItemFactory fac = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(fac);
		upload.setHeaderEncoding("utf-8");
		List fileList = null;
		try {
			fileList = upload.parseRequest(req);
		} catch (FileUploadException ex) {
			return;
		}
		Iterator<FileItem> it = fileList.iterator();
		String name = "";
		String extName = "";
		String newName = "";
		String fieldName = "";
		while (it.hasNext()) {
			FileItem item = it.next();
			if (!item.isFormField()) {
				name = item.getName();
				System.out.println(name);
				fieldName = item.getFieldName();
				savePath += fieldName + "/";
				File f1 = new File(savePath);
				if (!f1.exists()) {
					f1.mkdirs();
				}
				// long size = item.getSize();
				// String type = item.getContentType();
				// System.out.println(size + " " + type);
				if (name == null || name.trim().equals("")) {
					continue;
				}
				// ��չ����ʽ��
				if (name.lastIndexOf(".") >= 0) {
					extName = name.substring(name.lastIndexOf("."));
				}
				newName = sdf.format(new Date());
				FileOutputStream out = new FileOutputStream(savePath + newName
						+ extName);
				InputStream in = null;
				try {
					in = item.getInputStream();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return;
				}
				try {
					byte buffer[] = new byte[8192];
					int len = 0;
					while ((len = in.read(buffer)) > 0) {
						out.write(buffer, 0, len);
					}
				} catch (Exception e) {
					// TODO: handle exception
				} finally {
					try {
						in.close();
						out.close();
					} catch (Exception e2) {
						// TODO: handle exception
					}

				}
			}

		}
		resp.getWriter().print(
				name + ",uploadFile/" + fieldName + "/" + newName + extName);
	}

}
